// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameTools extends cc.Component {

    public static gameTools: GameTools = null;

    tools: { [key: string]: cc.NodePool } = cc.js.createMap();

    onLoad() {
        GameTools.gameTools = this;
        for (const key in config.nodeToolPath) {
            this.tools[config.nodeToolPath[key]] = new cc.NodePool();
        }
    }

    public get(path: string) {
        if (config.prefabArr[path] == null) return;
        let getNode: cc.Node = null;
        if (this.tools[path].size() > 0) {
            getNode = this.tools[path].get();
        } else {
            getNode = cc.instantiate(config.prefabArr[path]);
        }
        getNode.active = true;
        return getNode;
    }

    public put(node: cc.Node, path: string) {
        node.active = false;
        this.tools[path].put(node);
    }
}
